#include once "mud-gui/widgets/GridLayout.bi"

namespace ui

type GridLayoutData
	declare constructor( byval x_ as ubyte, byval y_ as ubyte, byval x2_ as ubyte, byval y2_ as ubyte )
	as ubyte x, y, x2, y2
end type

constructor GridLayoutData( byval x_ as ubyte, byval y_ as ubyte, byval x2_ as ubyte, byval y2_ as ubyte )
	x = x_ : y = y_ : x2 = x2_ : y2 = y2_
end constructor

constructor GridLayout( byval parent as BaseWidget ptr = 0, byval grid_x as ubyte = 0, byval grid_y as ubyte = 0 )

	if grid_x > 0 AND grid_y > 0 then this.sizeGrid( grid_x, grid_y )

	if parent <> 0 then
		if parent->cl <> layout_t.noLayout then
			SetParent(parent)
		else
			ShowError("Cannot insert GridLayout into this widget.")
		end if
	else
		'we're the main widget apparently
		screencontrol( fb.GET_SCREEN_SIZE, (BaseObject).width, (BaseObject).height )
		(BaseObject).x = 0 : (BaseObject).y = 0
	end if

	setBgColor = RGB(&hC9,&hC9,&hC9)
	(BaseObject).id = ui.widgetids.gridlayout
	(BaseObject).cl = layout_t.canHaveLayout
	connectSignal(@this,resize,GridLayout.resize_handler)
	connectSignal(@this,repaint,GridLayout.PaintEvent)
	connectSignal(@this,destroy,GridLayout.Destroy)

end constructor

IMP_PROPERTY(GridLayout,bgColor,uinteger)

function GridLayout.PlaceWidget( byval wid as BaseWidget ptr, byval x as integer, byval y as integer, _
									byval x2 as integer = 0, byval y2 as integer = 0 ) as integer

	if x2 = 0 andalso y2 = 0 then
		if m_grid[((x-1)*m_y)+(y-1)] <> 0 then return 0
		m_grid[((x-1)*m_y)+(y-1)] = 1
	else
		if x2 = 0 or y2 = 0 then showError("0 coordinate not allowed in GridLayout")


	for x_ as integer = (x-1) to (x2-1)
		for y_ as integer = (y-1) to (y2-1)
			if m_grid[(x_*m_y)+y_] <> 0 then
				for _x as integer = (x-1) to x_
					for _y as integer = (y-1) to y_
						m_grid[(_x*m_y)+_y] = 0
					next
				next
				return 0
			else
				m_grid[(x_*m_y)+y_] = 1
			end if
		next
	next

	end if

	if wid->parent = 0 then
		(BaseObject).addChild( wid )
	else
		if wid->parent <> @this then
			wid->parent->deleteChild( wid )
			(BaseObject).addChild( wid )
		end if
	end if

	wid->parent_data = new GridLayoutData(x,y,x2,y2)
	wid->x = (BaseObject).x + (m_hpix * (x-1)) + 1
	wid->y = (BaseObject).y + (m_ypix * (y-1)) + 1
	wid->width = m_hpix * iif(x2=0,1,(x2 - x)+1) - 1
	wid->height = m_ypix * iif(y2=0,1,(y2 - y)+1) - 1
	var e = new _GUI_EVENT_
	e->id = gevent.resize
		SendEvent( wid, e )
	delete e
	return 1

end function

STATIC_SLOT(GridLayout.resize_handler)

	if ref->children <> 0 then

		var tmp = ref->children
		while 1
			if tmp = 0 then exit while
				cast(GridLayout ptr, ref)->placeWidget( tmp->_data, _
								cast(GridLayoutData ptr,tmp->_data->parent_data)->x, _
								cast(GridLayoutData ptr,tmp->_data->parent_data)->y, _
								cast(GridLayoutData ptr,tmp->_data->parent_data)->x2, _
								cast(GridLayoutData ptr,tmp->_data->parent_data)->y2 )
				tmp = tmp->_next
		wend

	end if

	return 0

ENDSLOT

sub GridLayout.sizeGrid( byval grid_x as integer, byval grid_y as integer )

	if grid_x = 0 OrElse grid_y = 0 then ShowError("Error: GridLayout must be at least 1x1")
	m_x = grid_x
	m_y = grid_y
	m_grid = callocate( sizeof(ubyte) * (m_y * m_x) )
	if m_grid = 0 then ShowError( "Error allocating memory in GridLayout.sizeGrid" )
	m_hpix = int((BaseObject).width/m_x)
	m_ypix = int((BaseObject).height/m_y)

end sub

STATIC_SLOT(GridLayout.Destroy)

	var x = cast(GridLayout ptr, ref)

	if x->m_grid <> 0 then
		deallocate x->m_grid
		x->m_grid = 0
	end if

	return 1

ENDSLOT

STATIC_SLOT(GridLayout.PaintEvent)

	if ref->buffer = 0 then
		ref->buffer = imagecreate(ref->width, ref->height, Cast(GridLayout ptr, ref)->bgColor)
	end if
	
	put (ref->x, ref->y), ref->buffer, TRANS

	return 0

ENDSLOT


end namespace
